package project;

import java.util.ListIterator;

import collect.*;
import dbaccess.dbInteract;

public class Main {
	
	public static void main(String[] args) {

		// Initialize stocks and markets from database
		StockCollections sc = new StockCollections();
		MarketCollections mc = new MarketCollections();
		
		// TODO: Create a timing mechanism to check the prices periodically.
		//obtainCurrentMarketConditions(sc, mc);
		
		// TODO: Create a mechanism to obtain the final market condition at closing
		obtainFinalMarketConditions(sc, mc);
	}
	
	private static void obtainCurrentMarketConditions(StockCollections sc, MarketCollections mc){
		ListIterator<StockPairs> si = sc.getStocks().listIterator();
		ListIterator<MarketPairs> mi = mc.getMarkets().listIterator();
		
		//Iterates though the linked list of StockPairs. For each stock we have in the database,
		//this will go through and parse the information from the Yahoo! Finance page for that stock
		while(si.hasNext()){
			StockPairs s = si.next();
			
			//Prints the name of the stock in the console
			System.out.println(s.getName());
			try {
				//Info will contain the current value of the stock and the current trading volume
				StockInfo info = InfoCollector.getCurrentStockInfo(s.getSymbol());
				
				//Displays the parsed info from the webpage in the console
				info.showCurrentValues();
				
				// Store current stock information into the database
				int curr_sid = dbInteract.dbGetSID(s.getSymbol());
				dbInteract.dbInsertCurrStock(curr_sid, info.getLastTrade(), info.getVolume(), info.getChange());
				
			} catch (Exception e) {e.printStackTrace();}
		}
		
		//Iterates though the linked list of MarketPairs. For each market we have in the database,
		//this will go through and parse the information from the Yahoo! Finance page for that market
		while(mi.hasNext()){
			MarketPairs m = mi.next();
			
			//Prints the name of the market in the console
			System.out.println(m.getName());
			try {
				
				//Info will contain the current value of the market and the latest change
				MarketInfo info = InfoCollector.getCurrentMarketInfo(m.getSymbol());
				
				//Displays the parsed info from the webpage in the console
				info.showCurrentValues();
				
				// Store current market information into the database
				int curr_iid = dbInteract.dbGetIID(m.getSymbol());
				dbInteract.dbInsertCurrIndex(curr_iid, info.getCurrent(), info.getChange());
				
			} catch (Exception e) {e.printStackTrace();}
		}
	}
	
	private static void obtainFinalMarketConditions(StockCollections sc, MarketCollections mc){
		ListIterator<StockPairs> si = sc.getStocks().listIterator();
		ListIterator<MarketPairs> mi = mc.getMarkets().listIterator();
		
		//Iterates though the linked list of StockPairs. For each stock we have in the database,
		//this will go through and parse the information from the Yahoo! Finance page for that stock
		while(si.hasNext()){
			StockPairs s = si.next();
			
			//Prints the name of the stock in the console
			System.out.println(s.getName());
			try {
				//Info will contain opening price for the day, closing price of the day,
				//highest price of the day, lowest price of the day, latest trade volume,
				//change in price, the pe, and the eps
				StockInfo info = InfoCollector.getFinalStockInfo(s.getSymbol());
				
				//Displays the parsed info from the webpage in the console
				info.showFinalValues();
				
				// Store final-of-the-day stock information into the database
				int curr_sid = dbInteract.dbGetSID(s.getSymbol());
				dbInteract.dbInsertFinalStock(curr_sid, info.getOpen(), info.getClose(), info.getHigh(), info.getLow(), info.getVolume(), info.getChange(), info.getPE(), info.getEPS());
				
			} catch (Exception e) {e.printStackTrace();}
		}
		
		//Iterates though the linked list of MarketPairs. For each market we have in the database,
		//this will go through and parse the information from the Yahoo! Finance page for that market
		while(mi.hasNext()){
			MarketPairs m = mi.next();
			
			//Prints the name of the market in the console
			System.out.println(m.getName());
			try {
				
				//Info will contain opening value for the day, closing value for the day, and the
				//change in value
				MarketInfo info = InfoCollector.getFinalMarketInfo(m.getSymbol());
				
				//Displays the parsed info from the webpage in the console
				info.showFinalValues();
				
				// Store final-of-the-day market information into the database
				int curr_iid = dbInteract.dbGetIID(m.getSymbol());
				dbInteract.dbInsertFinalIndex(curr_iid, info.getOpen(), info.getClose(), info.getChange());
				
			} catch (Exception e) {e.printStackTrace();}
		}
	}

}
